<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="lzit.entity.Shanghus" %>
<%@ page import="lzit.dao.ShanghusDao" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>显示商户信息</title>
</head>
<body>
<form action="" method="post">
    <table width="90%" border="0" align="center">
        <tr>
            <td align="right">ID:</td>
            <td><input type="text" name="id"></td>
            <td align="right">区域：</td>
            <td><input type="text" name="qy"></td>
            <td align="right"><input type="radio" name="px" value="asc" checked>ID升序</td>
            <td><input type="radio" name="px" value="desc">ID降序<br></td>
            <td><input name="" type="submit" value="提交"></td>
        </tr>
    </table>
</form>
<%
    try {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String sid = request.getParameter("id");
        String qy = request.getParameter("qy");
        String px = request.getParameter("px");

        String sql = "SELECT * FROM SHANGHUS";

        List<String> conditions = new ArrayList<>();

        if (sid != null && !sid.trim().isEmpty()) {
            conditions.add("id=" + sid);
        }
        if (qy != null && !qy.trim().isEmpty()) {
            conditions.add("sh_quyu LIKE '%" + qy + "%'");
        }

        if (!conditions.isEmpty()) {
            sql += " WHERE " + String.join(" AND ", conditions);
        }

        if (px != null && !px.trim().isEmpty()) {
            sql += " ORDER BY id " + px;
        }

        ShanghusDao dao = new ShanghusDao();
        List<Shanghus> shanghuslist = null;

        try {
            shanghuslist = dao.findAllSh(sql);
        } catch (Exception e) {
            out.println("查询数据时发生错误：" + e.getMessage());
            return;
        }

        if (shanghuslist == null) {
            out.println("没有找到任何记录");
            return;
        }
%>
<table width="90%" border="0">
    <tr>
        <td align="center">商户列表</td>
    </tr>
    <tr>
        <td align="center">
            <table width="100%" border="1">
                <tr align="center">
                    <th scope="col">编号</th>
                    <th scope="col">商户名称</th>
                    <th scope="col">地区</th>
                    <th scope="col">联系人</th>
                    <th scope="col">电话</th>
                    <th scope="col">操作</th>
                </tr>
                <%
                    if (!shanghuslist.isEmpty()) {
                        for (Shanghus m : shanghuslist) {
                %>
                <tr align="center">
                    <td><%=m.getId()%></td>
                    <td><%=m.getSh_name()%></td>
                    <td><%=m.getSh_quyu()%></td>
                    <td><%=m.getSh_lianxiren()%></td>
                    <td><%=m.getSh_dianhua()%></td>
                    <td>
                        <a href="editShanghu.jsp?id=<%=m.getId()%>">更新</a>
                        <a href="deleteShanghu.jsp?id=<%=m.getId()%>">删除</a>
                    </td>
                </tr>
                <%
                        }
                    } else {
                        out.println("<tr><td colspan='6' align='center'>没有满足条件的记录</td></tr>");
                    }
                %>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="6" align="center">
            <p>
                <a href="addShanghu.jsp">添加</a>
            </p>
        </td>
    </tr>
</table>
<%
    } catch (Exception e) {
        out.println("发生错误：" + e.getMessage());
    }
%>
</body>
</html>